package com.mingmen.canting.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.loader.ImageLoader;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.lzy.imagepicker.view.CropImageView;
import com.mingmen.canting.R;
import com.mingmen.canting.http.MyOkhttp;
import com.mingmen.canting.http.callback.StringCallback;
import com.mingmen.canting.httpconfig.AppConfig;
import com.mingmen.canting.httpconfig.HttpResponse;
import com.mingmen.canting.imageloader.GlideImageLoader;
import com.mingmen.canting.util.SharePreferenceUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;


public class PicSelectActivity extends Activity implements View.OnClickListener {

    private ImagePicker imagePicker;
    ImageView testImage;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picselect);
        ButterKnife.bind(this);
//        img_getimg = findViewById(R.id.img_getimg);
        testImage = findViewById(R.id.testImage);
        imagePicker = ImagePicker.getInstance();

        imagePicker.setImageLoader(new GlideImageLoader());
        imagePicker.setMultiMode(true);
        imagePicker.setShowCamera(true);
        imagePicker.setCrop(false);
        imagePicker.setSaveRectangle(true);
        imagePicker.setStyle(CropImageView.Style.RECTANGLE);
        Integer width = Integer.valueOf(280);
        Integer height = Integer.valueOf(280);
        width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, width, getResources().getDisplayMetrics());
        height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, height, getResources().getDisplayMetrics());
        imagePicker.setFocusWidth(width);
        imagePicker.setFocusHeight(height);
        imagePicker.setOutPutX(Integer.valueOf(800));
        imagePicker.setOutPutY(Integer.valueOf(800));
        token = (String) SharePreferenceUtil.get(this, "token", "");

        Button btn_open_gallery = (Button) findViewById(R.id.btn_open_gallery);
        btn_open_gallery.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_open_gallery:


                Intent intent = new Intent(this, ImageGridActivity.class);
                intent.putExtra(ImageGridActivity.EXTRAS_IMAGES, images);
                //ImagePicker.getInstance().setSelectedImages(images);
                startActivityForResult(intent, 100);
                break;

        }
    }


    ArrayList<ImageItem> images = null;

    String path;

    List<File> myfile = new ArrayList<>();
    File file;
    String token;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            if (data != null && requestCode == 100) {
                images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                for (int i = 0; i < images.size(); i++) {
                    Log.d("dkdkdkk", images.get(i).path);
                    path = images.get(i).path;
                    file = new File(path);
                    myfile.add(file);
                }
                File file = new File(path);
//                File file = new File(path);
                Log.d("Myfile", myfile.toString());
//.params("file", multple)
                for (int i = 0; i < myfile.size(); i++) {
                    File multple = myfile.get(i);
                }
//                图片地址http://192.168.0.18/uploadimg/2020-08-22/d6401857-8280-4703-baec-256a5f9bdc24.jpeg
//                http://192.168.0.18/uploadimg/2020-08-29/fe40cc5a-f154-4ab6-b0fa-68f7b6718608.jpeg,/uploadimg/2020-08-29/0d4326b2-f1c2-43d4-b75f-29d1d09f9ecf.jpeg
//                MyOkhttp.post("http://192.168.3.18:9001/system/upload/img")
                MyOkhttp.post(AppConfig.IMGURL+"/system/upload/img")
                        .params("token", token)
                        .addFileParams("file", myfile)
                        .execute(new StringCallback(this) {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                Log.d("DKkdkdkd", s);

                            }
                        });
                Bitmap bm = BitmapFactory.decodeFile(images.get(0).path);
                testImage.setImageBitmap(bm);
            } else {
                Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
            }
        }
    }


}
